from app import app, db
from datetime import datetime
from werkzeug.security import generate_password_hash

# 定义模型（复制 app.py 中的模型定义）
class User(db.Model):
    __tablename__ = 'user'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)
    password_hash = db.Column(db.String(128))
    role = db.Column(db.String(20), nullable=False)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    is_active = db.Column(db.Boolean, default=True)
    
    __mapper_args__ = {
        'polymorphic_identity': 'user',
        'polymorphic_on': role
    }
    
    def set_password(self, password):
        self.password_hash = generate_password_hash(password)

class Student(User):
    __tablename__ = 'student'
    id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True)
    full_name = db.Column(db.String(100), nullable=False)
    student_id = db.Column(db.String(20), unique=True, nullable=False)
    major = db.Column(db.String(100), nullable=False)
    grade = db.Column(db.String(20))
    phone = db.Column(db.String(20))
    
    __mapper_args__ = {
        'polymorphic_identity': 'student',
    }

class Teacher(User):
    __tablename__ = 'teacher'
    id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True)
    full_name = db.Column(db.String(100), nullable=False)
    department = db.Column(db.String(100), nullable=False)
    title = db.Column(db.String(50))
    
    __mapper_args__ = {
        'polymorphic_identity': 'teacher',
    }

class Enterprise(User):
    __tablename__ = 'enterprise'
    id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True)
    company_name = db.Column(db.String(100), nullable=False)
    address = db.Column(db.String(200))
    contact_person = db.Column(db.String(100), nullable=False)
    phone = db.Column(db.String(20), nullable=False)
    description = db.Column(db.Text)
    status = db.Column(db.String(20), default='pending')
    
    __mapper_args__ = {
        'polymorphic_identity': 'enterprise',
    }

def create_test_users():
    with app.app_context():
        print("开始创建测试用户...")
        
        # 检查用户是否已存在
        if User.query.filter_by(username='student1').first():
            print('测试用户已存在，跳过创建')
            return
        
        try:
            # 创建测试学生
            student = Student(
                username='student1',
                email='student1@example.com',
                full_name='张三',
                student_id='2021001',
                major='计算机科学',
                grade='大三',
                role='student'
            )
            student.set_password('password123')
            
            # 创建测试教师
            teacher = Teacher(
                username='teacher1',
                email='teacher1@example.com',
                full_name='李老师',
                department='计算机学院',
                title='教授',
                role='teacher'
            )
            teacher.set_password('password123')
            
            # 创建测试企业
            enterprise = Enterprise(
                username='company1',
                email='company1@example.com',
                company_name='科技公司',
                contact_person='王经理',
                phone='13800138000',
                role='enterprise'
            )
            enterprise.set_password('password123')
            
            # 添加到数据库
            db.session.add(student)
            db.session.add(teacher)
            db.session.add(enterprise)
            db.session.commit()
            
            print('✅ 测试用户创建成功！')
            print('📝 学生账号: student1 / password123')
            print('👨‍🏫 教师账号: teacher1 / password123')
            print('🏢 企业账号: company1 / password123')
            
        except Exception as e:
            print(f'❌ 创建用户时出错: {e}')
            db.session.rollback()

if __name__ == '__main__':
    create_test_users()